package com.itheima.sys.controller;

import com.itheima.common.vo.Result;
import com.itheima.sys.dto.ChangeMainManagerDTO;
import com.itheima.sys.dto.CompanyUserAdminDTO;
import com.itheima.sys.dto.CompanyUserDTO;
import com.itheima.sys.service.ICompanyUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class CompanyUserController {

    @Autowired
    private ICompanyUserService companyUserService;


    /**
     * 根据用户ID查询企业员工记录
     * @param userId 系统用户ID
     * @param companyId 企业ID
     * @return
     */
    @GetMapping("/companyUser/query")
    public Result<List<CompanyUserDTO>> queryCompanyUserById(
            @RequestParam(value = "id", required = false) Long userId,
            @RequestParam(value = "companyId", required = false) Long companyId
    ){
        return Result.success(companyUserService.queryCompanyUserById(userId, companyId));
    }


    /**
     * 查询当前企业中管理员用户列表（返回员工的角色信息）
     * @return
     */
    @GetMapping("/company/subAdmin")
    public Result<List<CompanyUserDTO>> queryCompanyAdmins(){
        return Result.success(companyUserService.queryCompanyAdmins());
    }


    /**
     * 添加管理员用户-给用户设置角色
     * @param companyUserAdminDTO 用户角色信息
     * @return
     */
    @PreAuthorize("hasAuthority('company_add_admin')")
    @PostMapping("/company/subAdmin")
    public Result addSubAdmin(@RequestBody CompanyUserAdminDTO companyUserAdminDTO){
        companyUserService.addSubAdmin(companyUserAdminDTO);
        return Result.success();
    }


    /**
     * 查询当前企业主管理员
     * @return
     */
    @GetMapping("/company/getCurrentAdmin")
    public Result<CompanyUserDTO> getCurrentAdmin() {
        return Result.success("查询成功", companyUserService.getCurrentAdmin(null));
    }

    /**
     * 变更主管理员
     * @param changeMainManagerDTO
     * @return
     */
    @PostMapping("/company/config/admin")
    public Result changeSysAdmin(@RequestBody ChangeMainManagerDTO changeMainManagerDTO){
        companyUserService.changeSysAdmin(changeMainManagerDTO);
        return Result.success();
    }

    /**
     * 根据员工ID(主键)进行查询员工信息
     * @param companyUserId 员工ID
     * @return
     */
    @GetMapping("/companyUser/queryone")
    public Result<CompanyUserDTO> queryCompanyUserOne(@RequestParam(value = "id") Long companyUserId){
        return Result.success(companyUserService.queryCompanyUserOne(companyUserId));
    }

    /**
     * 根据员工ID集合查询员工集合
     * @param companyUserIds
     * @return
     */
    @GetMapping("/companyUser/queryByIds")
    public Result<List<CompanyUserDTO>> queryCompanyUserByIds(@RequestParam("ids") List<Long> companyUserIds){
        return Result.success(companyUserService.queryCompanyUserByIds(companyUserIds));
    }

    /**
     * 查询当前企业下包含所有员工
     * @return
     */
    @GetMapping("/companyUser/list")
    public Result<List<CompanyUserDTO>> queryCurrentAllQuery(){
        return Result.success(companyUserService.queryCurrentAllQuery());
    }

}
